package com.example.comment.repository;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.comment.base.dto.CommentQueryDTO;
import com.example.comment.base.entity.Comment;
import com.example.comment.base.vo.CommentVO;
import com.example.comment.enums.AuditStatusEnum;
import com.example.comment.mapper.CommentMapper;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Objects;

/**
 * @author QingZeyu
 * @date 2024/5/6 11:39
 */
@Repository
public class CommentRepository extends ServiceImpl<CommentMapper, Comment> {

    public Page<Comment> page(CommentQueryDTO dto) {
        Page<Comment> page = new Page<>(dto.getPage(), dto.getSize());
        return new LambdaQueryChainWrapper<Comment>(this.baseMapper)
                .isNull(Comment::getParentId)
                .isNull(Comment::getRootId)
                .eq(Comment::getAuditStatus, AuditStatusEnum.PASS.name())
                .eq(Comment::getReferenceId, dto.getReferenceId())
                .eq(Comment::getShowStatus,Boolean.TRUE)
                .eq(Comment::getDelFlag,Boolean.FALSE)
                .gt(Objects.nonNull(dto.getMaxCommentId()),Comment::getId,dto.getMaxCommentId())
                .orderByDesc(Comment::getCreateTime)
                .page(page);
    }

    public Page<Comment> getReplyComment(Long id,Integer pageNum,Integer sizeNum) {
        Page<Comment> page = new Page<>(pageNum, sizeNum);
        return new LambdaQueryChainWrapper<Comment>(this.baseMapper)
                .eq(Comment::getParentId, id)
                .eq(Comment::getRootId,id)
                .page(page);
    }

    public Comment getComment(Long id) {
        return new LambdaQueryChainWrapper<Comment>(this.baseMapper)
                .eq(Comment::getId,id)
                .eq(Comment::getAuditStatus, AuditStatusEnum.PASS.name())
                .eq(Comment::getShowStatus,Boolean.TRUE)
                .eq(Comment::getDelFlag,Boolean.FALSE)
                .one();
    }
}
